package com.meshnetics.gb.stack.mac.type;

import com.meshnetics.gb.type.DataValue;
import com.meshnetics.gb.type.impl.custom.ListDataValue;
import com.meshnetics.gb.type.impl.zcl.TypeUInt;
import com.meshnetics.gb.type.impl.zcl.UInt;

import java.nio.ByteBuffer;

/**
 * List of energy levels on channels - result of energy detect scan.
 *
 * @author AKovylin
 */
public class EnergyDetectList extends ListDataValue {
    public EnergyDetectList(Number[] elements) {
        super(TypeEnergyDetectList.TYPE, toUInts(elements));
    }

    private static DataValue[] toUInts(Number[] elements) {
        DataValue[] res = new DataValue[elements.length];
        for (int i = 0; i < elements.length; i++) {
            res[i] = (UInt) TypeUInt.UINT_8.fromObject(elements[i]);
        }
        return res;
    }

    public EnergyDetectList(UInt[] elements) {
        super(TypeEnergyDetectList.TYPE, elements);
    }

    EnergyDetectList(TypeEnergyDetectList listType, DataValue[] elements) {
        super(listType, elements);
    }

    public int getDataSize() {
        return getListSize();
    }

    public byte[] listMeasurements() {
        byte[] res = new byte[getListSize()];
        for (int i = 0; i < res.length; i++) {
            res[i] = ((Number) ((UInt) getElement(i)).toObject()).byteValue();
        }
        return res;
    }

    public Object toObject() {
        throw new UnsupportedOperationException("not implemented yet");
        // todo reimplement
    }

    public void serialize(ByteBuffer bb) {
        throw new UnsupportedOperationException("unsupported, serialization not expected");
    }
}
